package gruppe7.servlets;

import gruppe7.control.ServletController;
import gruppe7.model.Model;
import gruppe7.model.Station;
import gruppe7.model.User;
import gruppe7.util.Html;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EditStationInfoServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
		response.getWriter().println("OK POST");
	}
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
		Model model = Model.getInstance();
		User user = ServletController.getInstance().getSession(request, response).getUser();
		Html html = new Html();
        PrintWriter out = response.getWriter();
        RequestDispatcher rd = request.getRequestDispatcher("main_links.jsp");

        out.println(html.setHeader("Edit station info"));
        out.println("<body>");
        rd.include(request, response);
        
        out.println("<div id ='content'>");
        out.println("<div id='content-left'>");
        out.println("<h3><img src='images/tickets.png' /> Edit station info</h3><br/>"); 
        
        if (user.isAdminUser()) {
        	out.println(""
        			+ "<p><table class='default'>"
                    + "<form action='edit_station.do' method='post'>"
                    + "<tr><td>Choose station : </td>"
                    + "<td><select name='station'>");
        	
        	try {
        		for (String stationName : model.getStationList()) {
        			out.println("<option value='" + stationName + "'>" + stationName + "</option>");
        		}
        		
        		out.println("</select></td></tr></br>"
                        + "<tr><td></td><td align='right'><input type='submit' value='Edit' class='submit'/></td></tr>");
                out.println("</table>");
        	}
        	
        	catch (Exception e) {
        		out.println("Database-fejl: " + e);
        	}
        	
        }
        
        else out.println("You have to be an administrator to use this feature");
        
        out.println("</div></div>");
        out.println(html.setFooter());

	}

}
